package com.daotingli.backendprograms.Repository.Admin.AuthorBook;

import com.daotingli.backendprograms.Bean.Bean.bookBean;
import com.daotingli.backendprograms.Bean.Bean.t_userBean;
import com.daotingli.backendprograms.DB.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AuthorBookRepository {
    @Autowired
    private SpringDB  springDB;
    @Autowired
    private bookBean bookBean;
    @Autowired
    private t_userBean t_userBean;


    public List<bookBean> getAllAuthorBooks() {
        String sql  = "select * from book";
        List<bookBean> list =new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql,(rs,rowNum)->{
                bookBean = new bookBean(rs.getInt("id"), rs.getString("book_name"), rs.getString("author_id"), rs.getString("book_state"));
                list.add(bookBean);
                return bookBean;
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<t_userBean> getAllAuthors() {
        String sql  = "select * from t_user where identity = 2";
        List<t_userBean> list =new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql,(rs,rowNum)->{
                t_userBean = new t_userBean(rs.getInt("id"), rs.getString("username"), rs.getString("password"), rs.getInt("identity"), rs.getString("userphone"), rs.getInt("userstate"));
                list.add(t_userBean);
                return t_userBean;
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public Map<String, Object> addAuthorBook(String bookname, String authorId, String bookState) {
        String sql  = "insert into book(book_name,author_id,book_state) values(?,?,?)";
        Map<String, Object> map =new HashMap<>();
        try {
            springDB.getJdbcTemplate().update(sql,bookname,authorId,bookState);
            map.put("status","success");
            map.put("message","添加成功");
        } catch (Exception e) {
            map.put("status","error");
            map.put("message","添加失败");
        }
        return map;
    }

    public Map<String, Object> deleteAuthorBook(Integer id) {
//        删除是讲book_state改为1
        String sql  = "update book set book_state = 1 where id = ?";
        Map<String, Object> map =new HashMap<>();
        try {
            springDB.getJdbcTemplate().update(sql,id);
            map.put("status","success");
            map.put("message","删除成功");
        } catch (Exception e) {
            map.put("status","error");
            map.put("message","删除失败");
        }
        return map;
    }

    public Map<String, Object> updateAuthorBook(Integer id, String bookname, String authorId, String bookState) {
        String sql  = "update book set book_name = ?,author_id = ?,book_state = ? where id = ?";
        Map<String, Object> map =new HashMap<>();
        try {
            springDB.getJdbcTemplate().update(sql,bookname,authorId,bookState,id);
            map.put("status","success");
            map.put("message","修改成功");
        } catch (Exception e) {
            map.put("status","error");
            map.put("message","修改失败");
        }
        return map;
    }

    public List<bookBean> getAllBooks() {

        // 未被删除是 book_state = 0
        String sql  = "select * from book where book_state = 0";
        List<bookBean> list =new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql,(rs,rowNum)->{
                bookBean = new bookBean(rs.getInt("id"), rs.getString("bookname"), rs.getString("author_id"), rs.getString("book_state"));
                list.add(bookBean);
                return bookBean;
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<bookBean> getAllNoDeleteBooks() {
        String sql  = "select * from book where book_state = 1";
        List<bookBean> list =new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql,(rs,rowNum)->{
                bookBean = new bookBean(rs.getInt("id"), rs.getString("bookname"), rs.getString("author_id"), rs.getString("book_state"));
                list.add(bookBean);
                return bookBean;
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
